//
//  AddressListViewController.m
//  shop
//
//  Created by 邵存将 on 17/5/30.
//  Copyright © 2017年 geek-zoo studio. All rights reserved.
//

#import "AddressListViewController.h"
#import "F0_AddressListBoard_iPhone.h"
#import "SelfAddressViewController.h"
#import "F1_NewAddressBoard_iPhone.h"
#import "AddressDao.h"
#import "AddSelfAddressViewController.h"


@interface AddressListViewController ()

@end

@implementation AddressListViewController{
     UISegmentedControl *segmentControl;
    F0_AddressListBoard_iPhone * board;
    SelfAddressViewController *selfaddressViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *segmentdatas=[NSArray arrayWithObjects:@"自提地址",@"收货地址", nil];
    segmentControl=
    [self setUISegmentedControl:segmentdatas nomalcolor:[UIColor whiteColor] selectedColor:HEX_RGB(0X5abff5)];
    [segmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    CGFloat  width = 76 * segmentdatas.count;
    segmentControl.frame = CGRectMake((self.frame.size.width  - width)/2, 7, width, 30);
    [self.navigationController.navigationBar addSubview:segmentControl];
    [self showBarButton:BeeUINavigationBar.RIGHT image:[UIImage imageNamed:@"nav_add.png"]];
    [self showBarButton:BeeUINavigationBar.LEFT image:[UIImage imageNamed:@"item_grid_header_arrow_icon.png"]];
    [self showSelfAddress];
    // Do any additional setup after loading the view.
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)segmentAction:(id)sender{
    
    NSInteger index=[sender selectedSegmentIndex];
    if(index == 0)
    {
        [self showSelfAddress];
    }
    else{
        [self showCusAddress];
    }
}


-(void)viewWillAppear:(BOOL)animated{
    segmentControl.hidden=NO;
    [selfaddressViewController reloadData];
    if (board) {
        [board viewWillAppear:NO];
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    segmentControl.hidden=YES;
}

-(void)dealloc{
    [segmentControl removeFromSuperview];
}

-(void)showSelfAddress{
    if (!selfaddressViewController) {
        selfaddressViewController=[[SelfAddressViewController alloc]init];
        [self addChildViewController:selfaddressViewController];
        selfaddressViewController.view.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        [self.view addSubview:selfaddressViewController.view];

    }
    selfaddressViewController.view.hidden = NO;
    board.view.hidden = YES;
}

-(void)showCusAddress{
    if (!board) {
        board = [F0_AddressListBoard_iPhone board];
        board.shouldGotoManage = NO;
        [self addChildViewController:board];
        board.view.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        [self.view addSubview:board.view];
    }
    selfaddressViewController.view.hidden = YES;
    board.view.hidden = NO;

}




-(UISegmentedControl *)setUISegmentedControl:(NSArray *)segmentdatas  nomalcolor:(UIColor *)nomalcolor selectedColor:(UIColor *)selectedColor{
    //    UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake((ScreenWidth - 200)/2, 9, 200, 26)];
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake((self.frame.size.width - 152)/2, 7, 152, 30)];
    // ELMUCTheme *theme=[ELMUCTheme sharedInstance];
    for (int i=0;i<segmentdatas.count;i++) {
        NSString *value=segmentdatas[i];
        [segmentControl insertSegmentWithTitle:value atIndex:i animated:NO];
    }
    [segmentControl setSelectedSegmentIndex:0];
    NSDictionary *selectDic = [NSDictionary dictionaryWithObjectsAndKeys:selectedColor,NSForegroundColorAttributeName,nil];
    [segmentControl setTitleTextAttributes:selectDic forState:UIControlStateSelected];
    NSDictionary *normalDic = [NSDictionary dictionaryWithObjectsAndKeys:nomalcolor,NSForegroundColorAttributeName,nil];
    [segmentControl setTitleTextAttributes:normalDic forState:UIControlStateNormal];
    segmentControl.backgroundColor = [UIColor clearColor];
//    segmentControl.tintColor=[APFSkin colorWithKey:@"navigation_title_frame_color"];
    segmentControl.backgroundColor = [UIColor clearColor];
    segmentControl.layer.cornerRadius = 8;
    return segmentControl;
}

ON_RIGHT_BUTTON_TOUCHED( signal )
{
    if (!board||board.view.hidden==YES) {
        AddSelfAddressViewController *addSelfAddressViewController=[[AddSelfAddressViewController alloc]init];
        [self.stack pushViewController:addSelfAddressViewController animated:YES];
    }else{
       F1_NewAddressBoard_iPhone *newAddressBoard_iPhone= [F1_NewAddressBoard_iPhone board];
       newAddressBoard_iPhone.shouldShowMessage=self.shouldShowMessage;
       [self.stack pushBoard:[F1_NewAddressBoard_iPhone board] animated:YES];
    }
}

ON_LEFT_BUTTON_TOUCHED( signal )
{
    [self.navigationController popViewControllerAnimated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
